#!/bin/bash


#export ROOT=`cd "$ROOT"; pwd` #export ROOT=`dirname "$0"`
export ROOT=$HOME/.nextide
cd $ROOT
source $ROOT/settings/env/define
user_name=$(whoami)

#check-dir $ROOT/tool

#if [ ! -d $ROOT/tool/funkgfw ]; then
#    echo-ask "Please choice your OS { 1:Mac OS X(intel) 2:Linux(amd64) }: "
#file_funkgfw="funkgfw.tar.gz"
#    case $REPLY in
#	1)
#	    file_funkgfw="funkgfw-mac.tar.gz"
#	    ;;
#        2)
#	    file_funkgfw="funkgfw-linux.tar.gz"
#	    ;;
#        *) 
#	    echo-error "invalid option"
#	    ;;
#    esac
#    if [ ! -f $ROOT/tool/$file ]; then 
#	cd $ROOT/tool
#	wget "http://funkgfw.googlecode.com/files/$file_funkgfw"
#    fi
#    tar -zxvf $ROOT/tool/$file_funkgfw -C $ROOT/tool
#    cd $ROOT
#fi

#killall funkgfw
#cd $ROOT/tool/funkgfw
#nohup ./funkgfw > $ROOT/funkgfw.log 2>&1 & 
#cd $ROOT
#git config --global http.proxy 127.0.0.1:8087

fuck-gfw() {
    echo-ask "Do you want edit the proxy.ini? (y/N)"
    if [[ $REPLY =~ ^[Yy]$ ]]; then
	emacs -q $DIR_NET_SOURCE/goagent/local/proxy.ini
	fi
    if [ ! `whoami` == "root" ];then
	echo -e $(tput setaf 6)"'goagent' require root denied:"$(tput sgr0)
	fi
    source $DIR_ENV/fuckgfw
    cd $ROOT
    }
sys-menu() {
    echo-tip "----------------------------------------"
    echo-debug "    U) Uninstall"
    echo-success "    1) Install"
    echo-tip "    2) Dist-Upgrade"
    echo-waiting "    3) Link emacs"
    echo-waiting "    4) Unink emacs"
    echo-tip "    5) Flush Module settings"
    echo-tip "    6) Flush Yas-auto-insert template"
    echo-tip "    --"
    echo-success "    7) Update net plugins"
    echo-success "    8) Update net sources"
    echo-success "    G) Update Golang settings"
    echo-success "    L) Update Lisp(Clozure CL)"
    echo-tip "    --"
    echo-warning "    F) Fuck GFW!!!"
    echo-warning "    W) Upload GoAgent!"
    echo-tip "    P) Play!"
    echo-tip "    H) Help"
    echo-error "    Q) Exit!"
    echo-tip "----------------------------------------"
  
    echo-ask $"Hello,$user_name. What's up?"
  
    case $REPLY in
	u | U)
	    source $DIR_ENV/uninstall
	    ;;
	1)
	    source $DIR_ENV/install
	    ;;
	2)
	    source $DIR_ENV/dist-upgrade
	    ;;
	3)
	    source $DIR_ENV/link
	    ;;
	4)
	    source $DIR_ENV/unlink
	    ;;
	5)
	    source $DIR_ENV/update-module
	    ;;
	6)
	    source $DIR_ENV/update-auto-insert-tpl
	    ;;
	7)
	    source $DIR_ENV/update-net-plugins
	    ;;
	8)
	    source $DIR_ENV/update-net-sources
	    ;;
	s | S)
	    source $DIR_ENV/update-net-sdks
	    ;;

	f | F)
	    fuck-gfw
	    ;;
	w | W)
	    cd $DIR_NET_SOURCE/goagent/server
	    python uploader.zip update ./
	    cd $ROOT
	    ;;
	g | G)
	    fuck-gfw
	    source $DIR_ENV/update-golang
	    ;;
	l | L)
	    source $DIR_ENV/update-ccl
	    ;;
	h | H)
	    echo-tip "Usage: \n ..."
	    ;;
	p | P)
	    source $DIR_ENV/game
	    ;;
	q | Q)
	    echo-tip "Bye~"
	    #rm -rf $HOME/.gitconfig
	    exit
	    ;;
	
	*)
	    echo-error "Your input is invalid!"
	    #sleep 0.5
	    sys-menu
	    ;;
    esac
    
    #sleep 1
    cd $ROOT
    sys-menu
}

if [ ! -d "$DIR_USER" ]; then
    echo-ask "You have not installed the $IDE_NAME, do you want to install it? (Y/n)"
    if [[ $REPLY =~ ^[^nN]*$ ]]; then
	source $DIR_ENV/install
	echo-success "Install complete! Please startup your emacs. Enjoy!"
    fi
else
    sys-menu
fi
